package com.bywyu.turnpage.view;

import java.util.ArrayList;
import java.util.List;

import com.bywyu.turnpage.utils.AndroidUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

public class TurnpageView extends FrameLayout {
	
	private String LOGTAG = "TurnpageView";
	private Context context;
	private CubeView cv;
	private int centerX = 160;
	//test
	private List<View> views = new ArrayList<View>();
	private int i = 0;
	private int index = 0;
	
	public TurnpageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}
	
	public TurnpageView(Context context) {
		super(context);
		initView(context);
	}
	
	private void initView(Context context) {
		this.context = context;
		setBackgroundColor(Color.WHITE);
//		test
		addViews();
	}
	
	//test
	private void addViews(){
		
		String str = "Updates to the framework API are designed so that the new " +
		"API remains compatible with earlier versions of the API. That is, " +
		"most changes in the API are additive and introduce new or repla" +
		"cement functionality. As parts of the API are upgraded, the old" +
		"er replaced parts are deprecated but are not removed, so that e" +
		"xisting applications can still use them. In a very small number of c" +
		"ases, parts of the API may be modified or removed, although typi" +
		"cally such changes are only needed to ensure API robustness and" +
		" application or system security. All other " +
		"API parts from earlier revisions are carried f" +
		"orward without modification.The framework API that " +
		"an Android platform delivers is specified using an integer identifier ca" +
		"lled  Each Android platform version supports exactly one API Level, although" +
		" support is implicit for all earlier API Levels (down to API Level 1). The initial" +
		" release of the Android platform provided API Level 1 and subsequent r" +
		"eleases have incremented the API Level." +
		"The following table specifies the API Level supported by each version " +
		"of the Android platform.Updates to the framework API are designed so that the new " +
		"API remains compatible with earlier versions of the API. That is, " +
		"most changes in the API are additive and introduce new or repla" +
		"cement functionality. As parts of the API are upgraded, the old" +
		"er replaced parts are deprecated but are not removed, so that e" +
		"xisting applications can still use them. In a very small number of c" +
		"ases, parts of the API may be modified or removed, although typi" +
		"cally such changes are only needed to ensure API robustness and" +
		" application or system security. All other " +
		"API parts from earlier revisions are carried f" +
		"orward without modification.The framework API that " +
		"an Android platform delivers is specified using an integer identifier ca" +
		"lled  Each Android platform version supports exactly one API Level, although" +
		" support is implicit for all earlier API Levels (down to API Level 1). The initial" +
		" release of the Android platform provided API Level 1 and subsequent r" +
		"eleases have incremented the API Level." +
		"The following table specifies the API Level supported by each version " +
		"of the Android platform.";
		
		int width = 480, height = 854;
		
		DisplayMetrics display = AndroidUtil.getDisplayMetrics(context);
		width = display.widthPixels;
		height = display.heightPixels;
		
		TextView test01 = new TextView(context);
		test01.setBackgroundColor(Color.WHITE);
		test01.setTextColor(Color.RED);
		test01.setText("[1]"+str);
		test01.setLayoutParams(new LayoutParams(width,height));
		
		TextView test02= new TextView(context);
		test02.setBackgroundColor(Color.GREEN);
		test02.setText("[2]"+str);
		test02.setLayoutParams(new LayoutParams(width,height));
		
		TextView test03= new TextView(context);
		test03.setBackgroundColor(Color.YELLOW);
		test03.setText("[3]"+str);
		test03.setLayoutParams(new LayoutParams(width,height));
		
		TextView test04= new TextView(context);
		test04.setBackgroundColor(Color.WHITE);
		test04.setText("[4]"+str);
		test04.setLayoutParams(new LayoutParams(width,height));
		
		TextView test05= new TextView(context);
		test05.setBackgroundColor(Color.LTGRAY);
		test05.setText("[5]"+str);
		test05.setLayoutParams(new LayoutParams(width,height));
		
		views.add(test01);
		views.add(test02);
		views.add(test03);
		views.add(test04);
		views.add(test05);
		
		addView(views.get(i));
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		boolean isLR = false;
		switch(event.getAction()) {
		case MotionEvent.ACTION_DOWN :
			
			isLR = isLR(event.getX());
			Log.d(LOGTAG, "onTouchEvent MotionEvent.[ACTION_DOWN] [ 1 ] CubeView : i = "+ i +" isLR = "+isLR);
			removeViewAt(0);
			
			if(isLR){
				index =Math.max(i-1, 0);
			}else{
				index =Math.min(i+1, views.size() -1 );
			}
			
			Log.d(LOGTAG, "onTouchEvent MotionEvent.ACTION_DOWN [ 2 ] i = "+i +" index = "+index );
			
			addView(views.get(index),0);
			
			cv = new CubeView(context);
			List<Bitmap> bmps = new ArrayList<Bitmap>();
			View v = views.get(i);
			bmps.add(AndroidUtil.getBitmapFromView(v, v.getWidth(),  v.getHeight()));
			bmps.add(AndroidUtil.getBitmapFromView(views.get(index), v.getWidth(),  v.getHeight()));
			cv.setBmp(bmps);
			addView(cv,1);
			Log.d(LOGTAG, "onTouchEvent MotionEvent.ACTION_DOWN [ 3 ] i = "+i +" index = "+index );
			break;
		case MotionEvent.ACTION_UP :
			
			boolean isTurn = cv.isTurn();
			Log.d(LOGTAG, "onTouchEvent MotionEvent.[ACTION_UP] [ 4 ] index = "+index+" isTurn = "+isTurn);
			
			if(isTurn){
				
				removeViewAt(1);
				i = index;
			}else{
				
				removeAllViews();
				addView(views.get(i),0);
			}
			
			cv.destory();
			Log.d(LOGTAG, "onTouchEvent MotionEvent.[ACTION_UP] [ 5 ] i = "+i+" isTurn = "+isTurn);
			break;
		}
		return cv.onTouchEvent(event);
	}
	
	private boolean isLR(float x){
		boolean isLR = true;
			if(x >centerX){
				isLR = false;
			}
			return isLR;
	}
	
}
